package com.daiyilu.com.office4all.excel.read;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImageConverter implements Converter<String> {

    private static final Pattern pattern = Pattern.compile("_\\d+x\\d+");


    private static String getShortUrl(String url) {
        String minUrlSuffix = "_100x100.jpg";
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            url = url.replaceAll("_\\d+x\\d+\\.jpg", minUrlSuffix);
        } else if (url.endsWith("jpg")) {
            url += minUrlSuffix;
        }
        return url;
    }

    private static boolean isImage(String url) {
        url = url.toLowerCase(Locale.ROOT);
        return (url.startsWith("http://") || url.startsWith("https://")) && (url.endsWith(".jpg") || url.endsWith(".png") || url.endsWith(".jpeg"));
    }

    @Override
    public Class<String> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        return cellData.getStringValue();
    }

    @Override
    public CellData<String> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        return new CellData<>(value);
    }


}
